<template>
  <div class="tags">
    <el-tag size="large" v-for="(tag,index) in tags" :key="tag.name" :closable="tag.name !== 'home'" :disable-transitions="false" :effect="$route.name === tag.name ? 'dark' : 'plain'" @click="changeMenu(tag)"
    @close="handleClose(tag,index)">
      {{ tag.label }}
    </el-tag>
  </div>
</template>
<script>
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    //tab栏数据
    const tags = store.state.currentMenu
    //点击tab切换路由
    const changeMenu = (item) => {
      router.push({ name: item.name })
    }
    //根据name删除对应的路由
    const handleClose = (tag,index) => {
      let length = tags.length-1
      //处理vuex中的currentMenu
      store.commit('closeTab', tag)
      //做第一个判断
      //如果删除不是当前所高亮的标签，不做任何操作
      if (tag.name !== route.name)  return
      //做第二个判断
      //如果删除是当前所高亮的标签，且是最后一项，高亮往前推一位
      if(index === length){
        router.push({name:tags[index - 1].name})
      }else{
        router.push({name:tags[index].name})
         
      }
   
    }

    return {
      tags,
      changeMenu,
      handleClose
    }
  }
}
</script>
<style lang="less" scoped>
.tags {
  padding: 10px 0;
  width: 100%;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>